/**
 * ************************************************************************
 *
 * @file    log.h
 * @brief   LOG
 * @author  负号科技(fuhaokj@foxmail.com)
 * @version 0.1
 * @date    2024-08-23
 *
 * ************************************************************************
 *   __       _                 _    _
 *  / _|     | |               | |  (_)
 * | |_ _   _| |__   __ _  ___ | | ___
 * |  _| | | | '_ \ / _` |/ _ \| |/ / |
 * | | | |_| | | | | (_| | (_) |   <| |
 * |_|  \__,_|_| |_|\__,_|\___/|_|\_\ |
 *                                 _/ |
 *                                |__/
 *
 * @copyright Copyright (c) 2024 负号科技
 * For study and research only, no reprinting
 * ************************************************************************
 */
#ifndef LOG_H
#define LOG_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#define PRINT_COLOR 0    /*是否带颜色打印*/
#define LOG_WRITE_FILE 0 /*日志写入文件*/

#if (PRINT_COLOR)
#define ROUTINE_RED "\033[0;31m"
#define ROUTINE_GREEN "\033[0;32m"
#define ROUTINE_YELLOW "\033[0;33m"
#define ROUTINE_BLUE "\033[0;34m"
#define CTRL_RESET "\033[0m"
#else
#define ROUTINE_RED
#define ROUTINE_GREEN
#define ROUTINE_YELLOW
#define ROUTINE_BLUE
#define CTRL_RESET
#endif

#if (LOG_WRITE_FILE)
    int log_Init(char *file);

#else
#define log_Init(...)
#endif

#define LOG_BUF_SIZE 1024

    typedef long long (*get_sys_time_ms_def)(void);

    enum log_class
    {
        L_C_NULL = 0,
        L_C_EEEOR = 1, /*错误*/
        L_C_INFO = 2,  /*信息*/
        L_C_DEBUG = 3, /*调试*/
        L_C_WARN = 4   /*警告*/
    };

    void log_print(enum log_class class, const char *file, int line, const char *func, const char *fmt, ...);

#define LOG_DEBUG
// #undef LOG_DEBUG
#ifdef LOG_DEBUG

#define LOG_print(...) log_print(L_C_NULL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define LOG_error(...) log_print(L_C_EEEOR, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)            /*错误*/
#define LOG_info(...) log_print(L_C_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)              /*信息*/
#define LOG_debug(...) log_print(L_C_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)            /*调试*/
#define LOG_warn(...) log_print(L_C_WARN, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)              /*警告*/
#define LOG_Expr(Expr) log_print(L_C_INFO, __FILE__, __LINE__, __FUNCTION__, "%s : %d", #Expr, Expr); /*输出字符*/

/**
 * @brief   当值变化时，自定义字符 变量值
 * @date    2024-05-06
 *
 */
#define LOG_Change(str, Expr)                                                            \
    do                                                                                   \
    {                                                                                    \
        static uint32_t Expr##Change = 0;                                                \
        if (Expr##Change != Expr)                                                        \
        {                                                                                \
            log_print(L_C_INFO, __FILE__, __LINE__, __FUNCTION__, "%s : %d", str, Expr); \
            Expr##Change = Expr;                                                         \
        }                                                                                \
    } while (0)

/**
 * @brief   当值变化时，输出变量名 变量值
 * @date    2024-05-06
 *
 */
#define LOG_ExprChange(Expr) LOG_Change(#Expr, Expr)

#else
#define log_Init()
#define LOG_D(...)
#define LOG_error(...)
#define LOG_info(...)
#define LOG_debug(...)
#define LOG_warn(...)
#define LOG_Expr(Expr)
#define LOG_ExprChange(Expr)
#endif
#endif

#ifdef __cplusplus
}
#endif
